﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Recipe
{
    public class RecipieLike:IRecipeLike
    {
        public void LikeRecipe(int recipeId, int userId, bool like, ref Model.NutriLeafDBEntities context)
        {
            Model.RecipeLike likes = context.RecipeLikes.Where(p => p.RecipeID == recipeId && p.UserID == userId).First();
            likes.Liked = like;
            context.SaveChanges();
        }

        public void LikeRecipe(Model.Recipe recipe, Model.User user, bool like, ref Model.NutriLeafDBEntities context)
        {
            Model.RecipeLike recipie = context.RecipeLikes.Where(p => p.RecipeID == recipe.RecipeID && p.UserID == user.UserID).First();
            recipie.Liked = like;
        }

        public void RemoveRecipeLike(Model.RecipeLike like, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.RecipeLikes.Where(p => p.RecipeLikeID == like.RecipeLikeID).First());
            context.SaveChanges();
        }

        public List<Model.Recipe> GetRecipeLikedByUser(int userId, ref Model.NutriLeafDBEntities context)
        {
            List<Model.Recipe> r = new List<Model.Recipe>();
            List<Model.RecipeLike> like = context.RecipeLikes.Where(p => p.UserID == userId).ToList();
            foreach (Model.RecipeLike l in like) {
                r.Add(l.Recipe);
                
            }
            return r;
        }

        public void UpdateRecipeLike(Model.RecipeLike recipeLike, ref Model.NutriLeafDBEntities context)
        {
            Model.RecipeLike li = context.RecipeLikes.Where(p => p.RecipeLikeID == recipeLike.RecipeLikeID).First();
            li = recipeLike;
            context.SaveChanges();
        }
    }
}